/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.dao.impl;

import com.wisnu.scs.dao.DbBarangDao;
import com.wisnu.scs.entity.DbBarang;
import com.wisnu.scs.error.ErrorMessage;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class DbBarangDaoImpl implements DbBarangDao {

    private final SessionFactory sessionFactory;

    public DbBarangDaoImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void insertBarang(DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.save(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void updateBarang(DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.update(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void deleteBarang(DbBarang barang) throws ErrorMessage {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.delete(barang);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public List<DbBarang> selectBarangByName(String name) throws ErrorMessage {
        List<DbBarang> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarang where NAMA_BARANG LIKE :name")
                    .setString("name", "%" + name + "%").list();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

    @Override
    public DbBarang selectBarangById(Integer id) throws ErrorMessage {
        DbBarang barang = null;
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            barang = (DbBarang) session.createQuery("from DbBarang where ID_BARANG = :id ")
                    .setInteger("id", id)
                    .uniqueResult();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return barang;
    }

    @Override
    public List<DbBarang> selectAllBarang() throws ErrorMessage {
        List<DbBarang> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from DbBarang").list();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new ErrorMessage(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

}
